package com.ethink.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.system.api.constant.SystemRespCode;
import com.ethink.system.api.info.SystemMenuInfo;
import com.ethink.system.api.request.SystemMenuAddRequest;
import com.ethink.system.api.request.SystemMenuQueryRequest;
import com.ethink.system.api.request.SystemMenuUpdateRequest;
import com.ethink.system.common.entity.SystemMenu;
import com.ethink.system.common.entity.SystemRoleMenu;
import com.ethink.system.common.entity.SystemUser;
import com.ethink.system.common.mapper.SystemMenuMapper;
import com.ethink.system.common.mapper.SystemRoleMenuMapper;
import com.ethink.system.converter.SystemMenuConverter;
import com.ethink.system.service.SystemMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统菜单表 Service接口实现
 *
 * @author default
 * @date 2022-09-26
 */
@Slf4j
@Service
public class SystemMenuServiceImpl implements SystemMenuService {

    @Autowired
    private SystemMenuMapper systemMenuMapper;

    @Autowired
    private SystemMenuConverter systemMenuConverter;

    @Autowired
    private SystemRoleMenuMapper systemRoleMenuMapper;

    @Override
    public List<SystemMenuInfo> getSystemMenuList(SystemMenuQueryRequest request) {
        LambdaQueryWrapper<SystemMenu> wrapper = new LambdaQueryWrapper();
        if (ObjectUtil.isNotEmpty(request.getAppId())) {
            wrapper.eq(SystemMenu::getAppId, request.getAppId());
        }
        if (StrUtil.isNotBlank(request.getMenuName())) {
            wrapper.eq(SystemMenu::getMenuName, request.getMenuName());
        }
        if (ObjectUtil.isNotEmpty(request.getAppId())) {
            wrapper.eq(SystemMenu::getType, request.getType());
        }
        if (StrUtil.isNotBlank(request.getDateRangeBegin())) {
            wrapper.ge(SystemMenu::getCreateTime, request.getDateRangeBegin());
        }
        if (StrUtil.isNotBlank(request.getDateRangeEnd())) {
            wrapper.lt(SystemMenu::getCreateTime, request.getDateRangeEnd());
        }
        List<SystemMenu> systemMenuList = systemMenuMapper.selectList(wrapper);
        List<SystemMenuInfo> list = new ArrayList<>();
        for (SystemMenu systemMenu : systemMenuList) {
            SystemMenuInfo systemMenuInfo = systemMenuConverter.convertFrom(systemMenu);
            list.add(systemMenuInfo);
        }
        return list;
    }

    @Override
    public SystemMenuInfo getSystemMenuById(Long systemMenuId) {
        SystemMenu systemMenu = systemMenuMapper.selectById(systemMenuId);
        return systemMenuConverter.convertFrom(systemMenu);
    }

    @Override
    public void addSystemMenu(SystemMenuAddRequest request) {
        SystemMenu systemMenu = new SystemMenu();
        BeanUtils.copyProperties(request, systemMenu);
        systemMenuMapper.insert(systemMenu);
    }

    @Override
    public void updateSystemMenu(SystemMenuUpdateRequest request) {
        SystemMenu systemMenu = BeanUtil.copyProperties(request, SystemMenu.class);
        systemMenuMapper.updateById(systemMenu);
    }

    @Override
    public void deleteSystemMenu(Long systemMenuId) {
        systemMenuMapper.deleteById(systemMenuId);
        LambdaQueryWrapper<SystemRoleMenu> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SystemRoleMenu::getMenuId, systemMenuId);
        systemRoleMenuMapper.delete(wrapper);
    }
}
